package com.lechisoft.reflect.service;

import com.lechisoft.reflect.annotation.InitAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.TypeVariable;

/**
 * @author mister_wei
 * @version 1.1.1
 * @title web_service  天道有轮回,苍天饶过谁.
 * @package com.lechisoft.reflect.service
 * @date 2019/7/3 16:47
 */
public class BeanFactory  {
       private Object thisObj;

    public BeanFactory(String thisObj) {
        Class<?> aClass = null;
        try {
            aClass = Class.forName(thisObj);
            this.thisObj = aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public  Object initBean() {
            return Proxy.newProxyInstance(this.thisObj.getClass().getClassLoader(), this.thisObj.getClass().getInterfaces(), (o,method,objects) ->{
                Object invoke = null;
                for (Method method1 : this.thisObj.getClass().getMethods()) {
                    if (method1.getName().equals("init")) {
                        InitAnnotation annotation = method1.getAnnotation(InitAnnotation.class);
                        boolean notNull = annotation.isNotNull();
                        if (!notNull)
                            throw new Exception("不允许为false");
                    }
                }
                if (method.getName().equals("init")) {
                    InitAnnotation annotation1 = method.getAnnotation(InitAnnotation.class);

                    invoke = method.invoke(this.thisObj, objects);
                }
                return invoke;
            });
    }


}
